﻿using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media.Media3D;
using System.Windows.Threading;

namespace Whitman
{
    class KeyboardHook
    {
        #region win32

        public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);

        static int hKeyboardHook = 0; //声明键盘钩子处理的初始值
        public const int WH_KEYBOARD_LL = 13; //线程键盘钩子监听鼠标消息设为2，全局键盘监听鼠标消息设为13
        HookProc KeyboardHookProcedure; //声明KeyboardHookProcedure作为HookProc类型

        //键盘结构
        [StructLayout(LayoutKind.Sequential)]
        public class KeyboardHookStruct
        {
            public int vkCode; //定一个虚拟键码。该代码必须有一个价值的范围1至254
            public int scanCode; // 指定的硬件扫描码的关键
            public int flags; // 键标志
            public int time; // 指定的时间戳记的这个讯息
            public int dwExtraInfo; // 指定额外信息相关的信息
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct INPUT
        {
            public InputType dwType;
            public KEYBDINPUT ki;
        }

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        public struct KEYBDINPUT
        {
            public Int16 wVk;
            public Int16 wScan;
            public KEYEVENTF dwFlags;
            public Int32 time;
            public IntPtr dwExtraInfo;
        }

        public enum KEYEVENTF
        {
            EXTENDEDKEY = 1,
            KEYUP = 2,
            UNICODE = 4,
            SCANCODE = 8,
        }

        //使用此功能，安装了一个钩子
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);


        //调用此函数卸载钩子
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern bool UnhookWindowsHookEx(int idHook);


        //使用此功能，通过信息钩子继续下一个钩子
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);


        // 取得当前线程编号（线程钩子需要用到）
        [DllImport("kernel32.dll")]
        static extern int GetCurrentThreadId();


        //使用WINDOWS API函数代替获取当前实例的函数,防止钩子失效
        [DllImport("kernel32.dll")]
        public static extern IntPtr GetModuleHandle(string name);


        private const int WM_KEYDOWN = 0x100; //KEYDOWN
        private const int WM_KEYUP = 0x101; //KEYUP
        private const int WM_SYSKEYDOWN = 0x104; //SYSKEYDOWN
        private const int WM_SYSKEYUP = 0x105; //SYSKEYUP

        //ToAscii职能的转换指定的虚拟键码和键盘状态的相应字符或字符
        [DllImport("user32")]
        public static extern int ToAscii(int uVirtKey, //[in] 指定虚拟关键代码进行翻译。
            int uScanCode, // [in] 指定的硬件扫描码的关键须翻译成英文。高阶位的这个值设定的关键，如果是（不压）
            byte[] lpbKeyState, // [in] 指针，以256字节数组，包含当前键盘的状态。每个元素（字节）的数组包含状态的一个关键。如果高阶位的字节是一套，关键是下跌（按下）。在低比特，如果设置表明，关键是对切换。在此功能，只有肘位的CAPS LOCK键是相关的。在切换状态的NUM个锁和滚动锁定键被忽略。
            byte[] lpwTransKey, // [out] 指针的缓冲区收到翻译字符或字符。
            int fuState); // [in] Specifies whether a menu is active. This parameter must be 1 if a menu is active, or 0 otherwise.

        //获取按键的状态
        [DllImport("user32")]
        public static extern int GetKeyboardState(byte[] pbKeyState);


        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        private static extern short GetKeyState(int vKey);

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        private static extern short GetAsyncKeyState(int vKey);

        [DllImport("user32.dll")]
        internal static extern uint SendInput(uint nInputs,
            [MarshalAs(UnmanagedType.LPArray), In] INPUT[] pInputs,
            int cbSize);

        #endregion


        public event EventHandler<Keys> KeyUpEvent;
        public event Action<int> OnSpaced;
        public event Action OnBacked;
        public event Action<int> OnPaged;

        public void Start()
        {
            // 安装键盘钩子
            if (hKeyboardHook == 0)
            {
                KeyboardHookProcedure = new HookProc(KeyboardHookProc);

                hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure,
                    GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);

                //如果SetWindowsHookEx失败
                if (hKeyboardHook == 0)
                {
                    Stop();
                    throw new Exception("安装键盘钩子失败");
                }
            }
        }

        public void Stop()
        {
            bool retKeyboard = true;


            if (hKeyboardHook != 0)
            {
                retKeyboard = UnhookWindowsHookEx(hKeyboardHook);
                hKeyboardHook = 0;
            }

            if (!retKeyboard)
            {
                throw new Exception("卸载钩子失败！");
            }
        }

        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, string lParam);

        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();

        public void Send(string msg)
        {
            if (!string.IsNullOrEmpty(msg))
            {
                DoqffzpqkTcgchx = msg;
            }
        }

        private string DoqffzpqkTcgchx { set; get; }

        public bool IsStarted { set; get; }

        public event EventHandler DzkchqaTbzapmd;

        public event EventHandler KpxgbvyDjcpugw;

        private bool _hiqzgypkKpnc;

        /// <summary>
        /// 按键处理
        /// </summary>
        /// <param name="nCode"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns>如果返回1，则结束消息，这个消息到此为止，不再传递。如果返回0或调用CallNextHookEx函数则消息出了这个钩子继续往下传递，也就是传给消息真正的接受者</returns>
        private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
        {
            // 侦听键盘事件
            if (nCode >= 0)
            {
                if (wParam == WM_KEYUP && !string.IsNullOrEmpty(DoqffzpqkTcgchx) && !_hiqzgypkKpnc)
                {
                    KeyboardHookStruct hyvbSyfmmww =
                        (KeyboardHookStruct) Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));

                    var key = (Keys) hyvbSyfmmww.vkCode;
                    var cmo = Keyboard.Modifiers;

                    if ((key & Keys.Control) != 0 || (key == Keys.LControlKey))
                    {
                        cmo = cmo & (~ModifierKeys.Control);
                    }

                    if ((key & Keys.ShiftKey) != 0 || (key == Keys.LShiftKey))
                    {
                        cmo = cmo & (~ModifierKeys.Shift);
                    }

                    if (cmo == ModifierKeys.None&& _hiqzgypkKpnc == false)
                    {
                        _hiqzgypkKpnc = true;
                        Task.Run(async () =>
                        {
                            await Task.Delay(100);
                            Send();
                            _hiqzgypkKpnc = false;
                        });
                    }
                }
                if (wParam == 0x0100)
                {
                    KeyboardHookStruct myKeyboardHookStruct =
                        (KeyboardHookStruct) Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));

                    var key = (Keys) myKeyboardHookStruct.vkCode;

                    if (key == Keys.Oemcomma)
                    {
                        if (Keyboard.Modifiers.HasFlag(ModifierKeys.Control))
                        {
                            if (Keyboard.Modifiers.HasFlag(ModifierKeys.Shift))
                            {
                                KpxgbvyDjcpugw?.Invoke(this, null);
                                return 1;
                            }

                            DzkchqaTbzapmd?.Invoke(this, null);
                            return 1;
                        }
                    }


                    return 0;
                }
            }

            return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
        }

        private void Send()
        {
            SendKeys.SendWait(DoqffzpqkTcgchx);
            DoqffzpqkTcgchx = null;
        }
    }
}